home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Utilities / Font Management Utility / espanol / Xerox Font Management Utility.msi / Data1.cab / F3725_Fmu.chm / skinsupport / madcaphighlighter.js < prev    next >
Text File  |  2009-07-16  |  6KB  |  192 lines

  1. // {{MadCap}} //////////////////////////////////////////////////////////////////
  2. // Copyright: MadCap Software, Inc - www.madcapsoftware.com ////////////////////
  3. ////////////////////////////////////////////////////////////////////////////////
  4. // <version>3.0.0.0</version>
  5. ////////////////////////////////////////////////////////////////////////////////
  6.  
  7. var gColorTable        = new Array( "#ffff66,#000000",
  8.                                  "#a0ffff,#000000",
  9.                                  "#99ff99,#000000",
  10.                                  "#ff9999,#000000",
  11.                                  "#ff66ff,#000000",
  12.                                  "#880000,#ffffff",
  13.                                  "#00aa00,#ffffff",
  14.                                  "#886800,#ffffff",
  15.                                  "#004699,#ffffff",
  16.                                  "#990099,#ffffff" );
  17. var gColorIndex        = 0;
  18. var gFirstHighlight    = null;
  19. var gSearchType        = null;
  20.  
  21. function FMCClearSearch( win )
  22. {
  23.     var highlights    = FMCGetElementsByClassRoot( win.document, "highlight" );
  24.     
  25.     for ( var i = 0; i < highlights.length; i++ )
  26.     {
  27.         var highlight    = highlights[i];
  28.         var innerSpan    = FMCGetChildNodeByTagName( highlight, "SPAN", 0 );
  29.         var text        = win.document.createTextNode( innerSpan.innerHTML );
  30.         
  31.         highlight.parentNode.replaceChild( text, highlight );
  32.     }
  33.     
  34.     gColorIndex = 0;
  35.     gFirstHighlight = null;
  36.     
  37.     // At this point, highlight nodes got replaced by text nodes. This causes nodes that were once single text nodes to
  38.     // become divided into multiple text nodes. Future attempts to highlight multi-character strings may not work
  39.     // because they may have been divided into multiple text nodes. To solve this, we merge adjacent text nodes.
  40.     
  41.     FMCMergeTextNodes( win.document.body );
  42. }
  43.  
  44. function FMCMergeTextNodes( node )
  45. {
  46.     for ( var i = node.childNodes.length - 1; i >= 1; i-- )
  47.     {
  48.         var currNode    = node.childNodes[i];
  49.         var prevNode    = currNode.previousSibling;
  50.         
  51.         if ( currNode.nodeType == 3 && prevNode.nodeType == 3 )
  52.         {
  53.             prevNode.nodeValue = prevNode.nodeValue + currNode.nodeValue;
  54.             node.removeChild( currNode );
  55.         }
  56.     }
  57.     
  58.     for ( var i = 0; i < node.childNodes.length; i++ )
  59.     {
  60.         FMCMergeTextNodes( node.childNodes[i] );
  61.     }
  62. }
  63.  
  64. function FMCApplyHighlight( win, root, term, color, caseSensitive )
  65. {
  66.     var re    = null;
  67.     
  68.     if ( gSearchType == "NGram" )
  69.     {
  70.         re = new RegExp( term, "g" + (caseSensitive ? "" : "i") );
  71.     }
  72.     else
  73.     {
  74.         re = new RegExp( "(^|\\s|[.,;!#$/:?'\"()[\\]{}|=+*_\\-\\\\])" + term + "($|\\s|[.,;!#$/:?'\"()[\\]{}|=+*_\\-\\\\])", "g" + (caseSensitive ? "" : "i") );
  75.     }
  76.  
  77.     for ( var i = root.childNodes.length - 1; i >= 0; i-- )
  78.     {
  79.         var node    = root.childNodes[i];
  80.         
  81.         FMCApplyHighlight( win, node, term, color, caseSensitive );
  82.         
  83.         if ( node.nodeType != 3 || node.parentNode.nodeName == "SCRIPT" )
  84.         {
  85.             continue;
  86.         }
  87.         
  88.         var currNode    = node;
  89.         var text        = currNode.nodeValue;
  90.         
  91.         for ( var match = re.exec( text ); match != null; match = re.exec( text ) )
  92.         {
  93.             var pos        = match.index + (gSearchType == "NGram" ? 0 : match[1].length);
  94.             var posEnd    = pos + term.length;
  95.             var span    = win.document.createElement( "span" );
  96.             
  97.             span.className = "highlight";
  98.             span.style.fontWeight = "bold";
  99.             span.style.backgroundColor = color.split( "," )[0];
  100.             span.style.color = color.split( "," )[1];
  101.             
  102.             var span2    = win.document.createElement( "span" );
  103.  
  104.             span2.className = "SearchHighlight" + (gColorIndex + 1);
  105.             span2.appendChild( win.document.createTextNode( text.substring( pos, posEnd ) ) );
  106.             
  107.             span.appendChild( span2 );
  108.             
  109.             currNode.nodeValue = text.substring( 0, pos );
  110.             currNode.parentNode.insertBefore( span, currNode.nextSibling );
  111.             currNode.parentNode.insertBefore( win.document.createTextNode( text.substring( posEnd, text.length ) ), span.nextSibling );
  112.             
  113.             currNode = currNode.nextSibling.nextSibling;
  114.             text = currNode.nodeValue;
  115.             
  116.             //
  117.             
  118.             if ( !gFirstHighlight )
  119.             {
  120.                 gFirstHighlight = span;
  121.             }
  122.             
  123.             //
  124.             
  125.             FMCUnhide( win, span );
  126.         }
  127.     }
  128. }
  129.  
  130. function FMCHighlight( win, term, color, caseSensitive )
  131. {
  132.     if ( term == "" )
  133.     {
  134.         return;
  135.     }
  136.  
  137.     FMCApplyHighlight( win, win.document.body, term, color, caseSensitive );
  138.     
  139.     // Scroll to first highlighted term
  140.     
  141.     if ( gFirstHighlight && gFirstHighlight.offsetTop > win.document.documentElement.clientHeight )
  142.     {
  143.         win.document.documentElement.scrollTop = gFirstHighlight.offsetTop;
  144.     }
  145. }
  146.  
  147. function FMCHighlightUrl( win )
  148. {
  149.     gColorIndex = 0;
  150.     gFirstHighlight = null;
  151.     
  152.     var url = win.document.location.search;
  153.     
  154.     if ( url.IsNullOrEmpty() )
  155.     {
  156.         return;
  157.     }
  158.     
  159.     url = decodeURIComponent( url );
  160.  
  161.     var pos        = url.indexOf( "SearchType" );
  162.     var ampPos    = -1;
  163.     
  164.     if ( pos >= 0 )
  165.     {
  166.         ampPos = url.indexOf( "&", pos );
  167.         gSearchType = url.substring( 1 + pos + "SearchType".length, ampPos >= 0 ? ampPos : url.length );
  168.     }
  169.     
  170.     pos = url.indexOf( "Highlight" );
  171.     
  172.     if ( pos >= 0 )
  173.     {
  174.         ampPos = url.indexOf( "&", pos );
  175.         
  176.         var highlight    = url.substring( 1 + pos + "Highlight".length, ampPos >= 0 ? ampPos : url.length );
  177.         var stems        = highlight.split( "||" );
  178.         
  179.         for ( var i = 0; i < stems.length; i++ )
  180.         {
  181.             var phrases    = stems[i].split( "|" );
  182.             
  183.             for ( var j = 0; j < phrases.length; j++ )
  184.             {
  185.                 FMCHighlight( win, phrases[j], gColorTable[gColorIndex], false );
  186.             }
  187.             
  188.             gColorIndex = (++gColorIndex) % 10;
  189.         }
  190.     }
  191. }
  192.